package com.sixmac.platform.controller;

import com.sixmac.common.base.BaseController;
import com.sixmac.common.model.system.User;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.plugins.dao.Md;
import com.sixmac.core.shiro.ShiroKit;
import com.sixmac.core.toolbox.Paras;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.kit.JsonKit;
import com.sixmac.module.Contract;
import com.sixmac.module.Room;
import com.sixmac.module.TenantRoom;
import com.sixmac.platform.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * RoomController
 * 2018-09-11 20:12:57
 * @author Blade
 */
@Controller
@RequestMapping("/landroldRoom")
public class LandlordRoomController extends BaseController {
	private static String CODE = "landroldRoom";
	private static String PREFIX = "t_room";
	private static String LIST_SOURCE = "room.list2";
	private static String BASE_PATH = "/platform/landroldRoom/";
	
	@Autowired
	RoomService service;
	
	@RequestMapping(KEY_MAIN)
	public String index(ModelMap mm) {
		mm.put("code", CODE);
		return BASE_PATH + "room.html";
	}

	@RequestMapping(KEY_ADD)
	public String add(ModelMap mm) {
		mm.put("code", CODE);
		return BASE_PATH + "room_add.html";
	}

	@RequestMapping(KEY_EDIT + "/{id}")
	public String edit(@PathVariable Integer id, ModelMap mm) {
		Room room = service.findById(id);
		mm.put("model", JsonKit.toJson(room));
		mm.put("id", id);
		mm.put("code", CODE);
		return BASE_PATH + "room_edit.html";
	}

	@RequestMapping(KEY_VIEW + "/{id}")
	public String view(@PathVariable Integer id, ModelMap mm) {
		Room room = service.findById(id);
		mm.put("model", JsonKit.toJson(room));
		mm.put("id", id);
		mm.put("code", CODE);
		return BASE_PATH + "room_view.html";
	}

	@ResponseBody
	@RequestMapping(KEY_LIST)
	public Object list() {
		Object userId = ShiroKit.getUser().getId();
		User user = Blade.create(User.class).findById(Integer.valueOf(userId.toString()));
		Paras paras = Paras.create().set("phone",user.getAccount());
		Object grid = paginate(Md.getSql(LIST_SOURCE,paras));
		return grid;
	}

	@ResponseBody
	@RequestMapping(KEY_SAVE)
	public AjaxResult save() {
		Room room = mapping(PREFIX, Room.class);
		boolean temp = service.save(room);
		if (temp) {
			return success(SAVE_SUCCESS_MSG);
		} else {
			return error(SAVE_FAIL_MSG);
		}
	}

	@ResponseBody
	@RequestMapping(KEY_UPDATE)
	public AjaxResult update() {
		Room room = mapping(PREFIX, Room.class);
		boolean temp = service.update(room);
		if (temp) {
			return success(UPDATE_SUCCESS_MSG);
		} else {
			return error(UPDATE_FAIL_MSG);
		}
	}

	@ResponseBody
	@RequestMapping(KEY_REMOVE)
	public AjaxResult remove(@RequestParam String ids) {
		List<TenantRoom> tenantRooms = Blade.create(TenantRoom.class).findBy("roomId in("+ids+") and status = 1",null);
		if (tenantRooms.size()>0){
			return error("删除失败，选择的房间已有租户入住");
		}
		Blade.create(TenantRoom.class).deleteBy("roomId in("+ids+")",null);
		Blade.create(Contract.class).deleteBy("roomId in("+ids+")",null);
		int cnt = service.deleteByIds(ids);
		if (cnt > 0) {
			return success(DEL_SUCCESS_MSG);
		} else {
			return error(DEL_FAIL_MSG);
		}
	}
}
